package com.xiaomili.halfsugar.fragment;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.activity.OnePicActivity;
import com.xiaomili.halfsugar.adapter.RecAdapter;
import com.xiaomili.halfsugar.utils.RecyclerItemClickListener;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/7/19 0019.
 */
public class PicFragment extends Fragment {

    @InjectView(R.id.rec_pic)
    RecyclerView recPic;
    private GridLayoutManager manager;
    private Activity mActivity;
    private List<String> mDatas;
    private RecAdapter mRecAdapter;
    private final static int SCAN_OK = 1;
    private String crmera = "file:///android_asset/ic_album_camera.png";
    //private ProgressDialog mProgressDialog;
    private HashMap<String, List<String>> mGruopMap = new HashMap<String, List<String>>();
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SCAN_OK:
                    // mProgressDialog.dismiss();
                    mRecAdapter = new RecAdapter(mActivity, mDatas);
                    recPic.setAdapter(mRecAdapter);
                    recPic.setLayoutManager(manager);
                    recPic.addOnItemTouchListener(new RecyclerItemClickListener(mActivity, new RecyclerItemClickListener.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            if (position == 0) {
                                Intent intent2 = new Intent();
                                intent2.setAction("android.media.action.STILL_IMAGE_CAMERA");
                                startActivity(intent2);
                            } else {
                                String s = mDatas.get(position);
                                Intent intent = new Intent(mActivity, OnePicActivity.class);
                                intent.putExtra("path", s);
                                startActivity(intent);
                            }
                        }
                    }));
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        getImages();
        manager = new GridLayoutManager(mActivity, 4,GridLayoutManager.VERTICAL,false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pic, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    private void getImages() {
        mDatas = new ArrayList<>();
        mDatas.add(crmera);
        // mProgressDialog = ProgressDialog.show(this,null,"正在加载...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                ContentResolver mContentResolver = mActivity.getContentResolver();
                Cursor mCursor = mContentResolver.query(mImageUri, null, MediaStore.Images.Media.MIME_TYPE + "=? or "
                                + MediaStore.Images.Media.MIME_TYPE + "=?",
                        new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED);
                if (mCursor == null) {
                    return;
                }
                while (mCursor.moveToNext()) {
                    //获取图片的路径
                    String path = mCursor.getString(mCursor
                            .getColumnIndex(MediaStore.Images.Media.DATA));

                    mDatas.add(path);

                    //获取该图片的父路径名
                    String parentName = new File(path).getParentFile().getName();
                    //根据父路径名将图片放入到mGruopMap中
                    if (!mGruopMap.containsKey(parentName)) {
                        List<String> chileList = new ArrayList<String>();
                        chileList.add(path);
                        mGruopMap.put(parentName, chileList);
                    } else {
                        mGruopMap.get(parentName).add(path);
                    }
                }
                mHandler.sendEmptyMessage(SCAN_OK);
                mCursor.close();
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
